# !/usr/bin/python
# -*- coding: utf-8 -*-

from 神方.util.date_util import *
from 神方.util.http_util import get_request

# 根据比较最后一条记录的日期来获取下一页的url
def get_last_date(trs, start_time):
    # 当前总条数
    num = len(trs)
    # 获取到最后一条的日期
    lastDate = trs[num - 1].span.string.strip()
    print('湖北省最后一条的日期是：%s' % lastDate)
    # 日期转化为int比较
    lastDate = time.mktime(time.strptime(lastDate, "%Y-%m-%d"))
    lastDiff = int(lastDate) - int(start_time)
    if lastDiff >= 0:
        return 1
    else:
        return 0

def get_hubei_value(url, pageNum, list_datas, start_time):
    soup = get_request(url)
    # 根据标签获取数据
    lis = soup.find(name='ul', attrs={'class': 'ewb-news-items'}).findAll('li')

    for i in lis:
        # 内容
        content = i.a['title']
        # 日期
        date = i.span.string.strip()
        # 判断该日期是否获取
        end_time = time.mktime(time.strptime(date, "%Y-%m-%d"))
        # 日期转化为int比较
        diff = int(end_time) - int(start_time)
        if diff >= 0:
            list_td = ['湖北省', '', date, '', content]
            list_datas.append(list_td)
        else:
            break


    flag = get_last_date(lis, start_time)
    if flag == 1:
        pageNum = pageNum + 1
        urlNext = 'http://www.hbggzy.cn/jydt/003001/003001002/' + str(pageNum) + '.html'
        get_hubei_value(urlNext, pageNum, list_datas, start_time)

    return list_datas

if __name__ == '__main__':
    url = 'http://www.hbggzy.cn/jydt/003001/003001002/moreinfo_jyxx.html'
    # 获得当前日期的前十天的日期
    now_time_str = get_date(5, '%Y-%m-%d')
    # mktime参数为struc_time,将日期转化为秒，
    start_time = get_offset(now_time_str, '%Y-%m-%d')

    # 创建一个空的列表用来存放数据
    list_datas = []
    pageNum = 0
    list_datas = get_hubei_value(url, pageNum, list_datas, start_time)

    print(list_datas)



